package com.wyh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyh.common.response.PageInfo;
import com.wyh.entity.AdminAttention;
import com.wyh.mapper.AdminAttentionMapper;
import com.wyh.request.AdminAttentionRequest;
import com.wyh.service.IAdminAttentionService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 关注功能业务层处理
 *
 * @author WangYuHang
 * @date 2024-03-03
 */
@Slf4j
@Service
@AllArgsConstructor
public class AdminAttentionServiceImpl implements IAdminAttentionService {

    private final AdminAttentionMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdminAttention add(AdminAttentionRequest request) {
        AdminAttention entity = AdminAttention.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdminAttentionRequest request) {
        AdminAttention entity = AdminAttention.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public AdminAttention getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminAttention> pageList(AdminAttentionRequest request) {
        Page<AdminAttention> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<AdminAttention> query = this.buildWrapper(request);
        IPage<AdminAttention> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<AdminAttention> list(AdminAttentionRequest request) {
        LambdaQueryWrapper<AdminAttention> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<AdminAttention> buildWrapper(AdminAttentionRequest param) {
        LambdaQueryWrapper<AdminAttention> query = new LambdaQueryWrapper<>();
        if (param.getUserId() != null){
               query.eq(AdminAttention::getUserId ,param.getUserId());
        }
        if (param.getFocusUserId() != null){
               query.eq(AdminAttention::getFocusUserId ,param.getFocusUserId());
        }
        if (param.getIsAttention() != null){
               query.eq(AdminAttention::getIsAttention ,param.getIsAttention());
        }
        if (param.getIsDelete() != null){
               query.eq(AdminAttention::getIsDelete ,param.getIsDelete());
        }
        if (param.getCreateUserId() != null){
               query.eq(AdminAttention::getCreateUserId ,param.getCreateUserId());
        }
        if (param.getUpdateUserId() != null){
               query.eq(AdminAttention::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
